//
//  RBTabBarController.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/5.
//

#import "RBTabBarController.h"
#import "RBNavigationController.h"
#import "RBCompeListController.h"
#import "RBExpertListController.h"
#import "RBMessageListController.h"
#import "RBMaterialListController.h"
#import "RBMineListController.h"

@interface RBTabBarController ()<UITabBarControllerDelegate>

@end

@implementation RBTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:@"RBLoginSuccess" object:nil];
    
    NSMutableDictionary *normalDict = [NSMutableDictionary dictionary];
    normalDict[NSForegroundColorAttributeName] = RBColor(51, 51, 51);
    normalDict[NSFontAttributeName] = RBPingFangMediumFont(12);
    NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary];
    selectedDict[NSForegroundColorAttributeName] = RBColor(242, 48, 48);
    selectedDict[NSFontAttributeName] = RBPingFangMediumFont(12);
    if (@available(iOS 13.0, *)) {
        UITabBarItemAppearance *itemAppearance = [[UITabBarItemAppearance  alloc] init];
        [itemAppearance.normal setTitleTextAttributes:normalDict];
        [itemAppearance.selected setTitleTextAttributes:selectedDict];
        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc] init];
        tabBarAppearance.stackedLayoutAppearance = itemAppearance;
        tabBarAppearance.backgroundColor = [UIColor whiteColor];
        tabBarAppearance.shadowColor = [UIColor whiteColor];
        self.tabBar.standardAppearance = tabBarAppearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = tabBarAppearance;
        }
    } else {
        UITabBarItem *tabBarItem = [UITabBarItem appearance];
        [tabBarItem setTitleTextAttributes:normalDict forState:UIControlStateNormal];
        [tabBarItem setTitleTextAttributes:selectedDict forState:UIControlStateSelected];
        [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
        [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
        [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    }
    
    [self rb_AddChildrenViewController:[[RBCompeListController alloc] init] andTitle:@"比赛" andImageName:@"bswxz" andSelectImageName:@"bsxz"];
    [self rb_AddChildrenViewController:[[RBExpertListController alloc] init] andTitle:@"专家" andImageName:@"zjwxz" andSelectImageName:@"zjxz"];
    [self rb_AddChildrenViewController:[[RBMessageListController alloc] init] andTitle:@"消息" andImageName:@"xxwxz" andSelectImageName:@"xxxz"];
    [self rb_AddChildrenViewController:[[RBMaterialListController alloc] init] andTitle:@"红料" andImageName:@"hlwxz" andSelectImageName:@"hlxz"];
    [self rb_AddChildrenViewController:[[RBMineListController alloc] init] andTitle:@"我的" andImageName:@"wdwxz" andSelectImageName:@"wdxz"];
}
- (void)rb_AddChildrenViewController:(UIViewController *)controller andTitle:(NSString *)titleStr andImageName:(NSString *)normalImageName andSelectImageName:(NSString *)selectedImageName{
    controller.tabBarItem.title = titleStr;
    controller.tabBarItem.image = [[UIImage imageNamed:normalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0,-3);
    RBNavigationController *navVc = [[RBNavigationController alloc] initWithRootViewController:controller];
    [self addChildViewController:navVc];
}
- (void)loginSuccess:(NSNotification *)notification{
    NSString *source = notification.userInfo[@"source"];
    if ([source isEqualToString:@"RBTabBarItemSource"]) {
        RBTabBarController *tabBarVc = (RBTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        tabBarVc.selectedIndex = [notification.userInfo[@"logSelect"] integerValue];
    }
}
@end
